package com.zhangc.blog.admin.service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangc.blog.admin.entity.BlogInfoEntity;
import com.zhangc.blog.admin.entity.BlogTagCount;
import com.zhangc.blog.admin.entity.BlogTagEntity;
import com.zhangc.blog.admin.entity.BlogTagRelationEntity;
import com.zhangc.blog.admin.mapper.BlogInfoMapper;
import com.zhangc.blog.admin.mapper.BlogTagMapper;
import com.zhangc.blog.base.constants.SysConfigConstants;
import com.zhangc.blog.base.enums.DeleteStatusEnum;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author: 南街
 * @since 2019-08-28
 */
@Service
public class BlogTagServiceImpl extends ServiceImpl<BlogTagMapper, BlogTagEntity> {

    @Autowired
    private BlogTagRelationServiceImpl blogTagRelationService;

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Autowired
    private BlogInfoServiceImpl blogInfoService;

    public List<BlogTagCount> getBlogTagCountForIndex() {
        QueryWrapper<BlogTagEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(BlogTagEntity::getIsDeleted, DeleteStatusEnum.NO_DELETED.getStatus());
        List<BlogTagEntity> list = baseMapper.selectList(queryWrapper);
        List<BlogTagCount> blogTagCounts = list.stream()
                .map(blogTag -> new BlogTagCount()
                        .setTagId(blogTag.getTagId())
                        .setTagName(blogTag.getTagName())
                        .setTagCount(
                                blogTagRelationService.count(new QueryWrapper<BlogTagRelationEntity>()
                                        .lambda().eq(BlogTagRelationEntity::getTagId, blogTag.getTagId()))
                        )).collect(Collectors.toList());
        return blogTagCounts;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean clearTag(Integer tagId) {
        LambdaQueryWrapper<BlogTagRelationEntity> queryWrapper = Wrappers.<BlogTagRelationEntity>lambdaQuery()
                .eq(BlogTagRelationEntity::getTagId, tagId);
        List<BlogTagRelationEntity> tagRelationList = blogTagRelationService.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(tagRelationList)) {
            // 批量更新的BlogInfo信息
            List<BlogInfoEntity> infoList = tagRelationList.stream()
                    .map(tagRelation -> new BlogInfoEntity()
                            .setBlogId(tagRelation.getBlogId())
                            .setBlogTags(SysConfigConstants.DEFAULT_TAG.getConfigName())).collect(Collectors.toList());
            List<Long> blogIds = infoList.stream().map(BlogInfoEntity::getBlogId).collect(Collectors.toList());
            // 批量更新的tagRelation信息
            List<BlogTagRelationEntity> tagRelations = tagRelationList.stream()
                    .map(tagRelation -> new BlogTagRelationEntity()
                            .setBlogId(tagRelation.getBlogId())
                            .setTagId(Integer.valueOf(SysConfigConstants.DEFAULT_CATEGORY.getConfigField())))
                    .collect(Collectors.toList());
            blogInfoService.updateBatchById(infoList);
            blogTagRelationService.remove(new QueryWrapper<BlogTagRelationEntity>()
                    .lambda()
                    .in(BlogTagRelationEntity::getBlogId, blogIds));
            blogTagRelationService.saveBatch(tagRelations);
        }
        return retBool(baseMapper.deleteById(tagId));
    }
}
